use spring_axum::{AppResult, AppError};

#[derive(Clone, Default)]
#[spring_axum_macros::component]
pub struct UserService;

#[spring_axum_macros::tx_service]
impl UserService {
    pub async fn save_profile_and_queries(&self, mapper: &crate::mappers::user_mapper::UserMapper, username: &str, email: &str) -> AppResult<()> {
        // Core save
        mapper.save_profile(username, email)?;

        // Example logic composition resides in service, not mapper
        mapper.search(Some(username), Some(email))?;
        mapper.find_by_ids(&[1, 2, 3])?;
        Ok(())
    }

    // 事务示例：先写入，再根据业务条件主动触发错误以演示回滚
    pub async fn save_profile_tx(&self, mapper: &crate::mappers::user_mapper::UserMapper, username: &str, email: &str) -> AppResult<()> {
        // 写入用户信息
        mapper.save_profile(username, email)?;
        // 如果 email 包含 "rollback"，模拟业务失败，触发回滚
        if email.contains("rollback") {
            return Err(AppError::BadRequest("force rollback".into()));
        }
        Ok(())
    }
}